<?php
namespace app\api\model;
use app\common\model\Dealer as DealerModel;
use think\Db;

/**
 * 分销商进出帐模型
 */
class Dealer extends DealerModel
{
    /**
     * 添加
     */
    public function add($user_id,$extract_money)
    {
		// 开启事务
        Db::startTrans();
        try {
			$user = (new User)->where(['user_id' => $user_id])->find();
			if ($user['commission']<$extract_money) {
				$this->error = '可提余额不足';
				return false;
			}
			$user->commission = ['dec', $extract_money]; //扣减用户佣金
			$user->save();
            // 添加提现记录
            $this->save([
				'mode' => 20,
				'user_id' => $user_id,
				'price' => $extract_money,
				'wxapp_id' => self::$wxapp_id
			]);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
        }
        return false;
    }

}
